package com.gsbelarus.gedemin.salary.activity;

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.MenuItem;

import com.gsbelarus.gedemin.lib.sync.protocol.entity.SyncServiceStatus;
import com.gsbelarus.gedemin.lib.ui.activity.SyncActivityModel;
import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.fragment.SettingsFragment;
import com.gsbelarus.gedemin.salary.util.GCMHelper;

public class Settings extends SyncActivityModel {

    private SettingsFragment fragment;

    @Override
    public int getViewResource() {
        return R.layout.activity_fragment;
    }

    @Override
    public void onReceiveSyncStatus(SyncServiceStatus syncServiceStatus, SyncActivityModel.SyncStatusDialog syncStatusDialog) {
        super.onReceiveSyncStatus(syncServiceStatus, syncStatusDialog);
        syncStatusDialog.show(syncServiceStatus);
        switch (syncServiceStatus.getTypeOfStatus()) {
            case START_SYNC:
                if (fragment != null && fragment.isAdded())
                    fragment.setEnabledAuthPref(false);
                break;
            case FINISH_SYNC:
                if (fragment != null && fragment.isAdded())
                    fragment.setEnabledAuthPref(true);
                break;
        }
    }

    @Override
    public void onCreateActivity(Bundle arguments) {
        super.onCreateActivity(arguments);

        if (getSupportActionBar() != null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        fragment = new SettingsFragment();
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame_content, fragment);
        fragmentTransaction.commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new GCMHelper(this).checkPlayServices(this);
    }
}
